package com.example.ustoreui;

import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.Resource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Label;
import com.vaadin.ui.Link;
import com.vaadin.ui.LoginForm;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.LoginForm.LoginEvent;

@SuppressWarnings("serial")
public class Login extends VerticalLayout {
	
	private LoginForm loginForm;
	private CheckBox manterConectadoChkBox;
	private Link recuperarSenhaLink;
	private Label direitosAutoraisLabel;
	private Panel loginPanel;
	
    public Login(Window mainWindow) {
    	buildLoginPanel();
    	onClickLoginButton(mainWindow);
    }
    
    private void buildLoginPanel() {
    	
    	loginForm = new LoginForm();
    	loginForm.setUsernameCaption("Login");
    	loginForm.setPasswordCaption("Senha");
    	loginForm.setLoginButtonCaption("Entrar");
    	
    	manterConectadoChkBox = new CheckBox("Manter-me conectado");
    	recuperarSenhaLink = new Link("Esqueci minha senha", new ExternalResource(""));
    	direitosAutoraisLabel = new Label("2001 U-Store Todos os direitos reservados");
        
        loginPanel = new Panel();
        loginPanel.setWidth("305px");
        loginPanel.addComponent(getLogo());
        loginPanel.addComponent(loginForm);
        loginPanel.addComponent(manterConectadoChkBox);
        loginPanel.addComponent(recuperarSenhaLink);
        loginPanel.addComponent(direitosAutoraisLabel);
        
        this.addComponent(loginPanel);
        this.setComponentAlignment(loginPanel, Alignment.MIDDLE_CENTER);
        
    }
    
    private Embedded getLogo() {
    	
    	Resource resource = new ThemeResource("img/logo_little.png");
        Embedded image = new Embedded(null, resource);
        image.setWidth("270px");
        image.setHeight("90px");
        
        return image;
    }
    
    private void onClickLoginButton(final Window mainWindow) {
    	loginForm.addListener(new LoginForm.LoginListener(){
    	    @Override
    	    public void onLogin(LoginEvent event) {
    	    	Window newWindow = mainWindow;
    	    	newWindow.removeAllComponents();
    	    	newWindow.addComponent(new Home(mainWindow));
    	    }      
    	});
    }
       
}
